package com.ka.datacenter;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.google.gson.Gson;
import com.ka.model.GeneralModel;
import com.ka.provider.Type.ModelType;

public class DatabaseHandler extends SQLiteOpenHelper {

	private static final int DATABASE_VERSION = 7;
	private static final String DATABASE_NAME = "LifeManagerment.db";

	private static final String TABLE_01 = "Activity";

	private Gson gson;

	public DatabaseHandler(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
		gson = new Gson();
	}

	// Creating Tables
	@Override
	public void onCreate(SQLiteDatabase db) {

		String CREATE_CONTACTS_TABLE = "CREATE TABLE "
				+ TABLE_01
				+ "("
				+ "id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, name TEXT, price INTEGER, time TEXT, price_type INTEGER )";
		db.execSQL(CREATE_CONTACTS_TABLE);
	}

	// Upgrading database
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_01);
		onCreate(db);
	}

	/**
	 * All CRUD(Create, Read, Update, Delete) Operations
	 */

	// Adding new model
	public Boolean addModel(GeneralModel model) {

		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = model.calculatorValue();
		if (db.insert(model.tableName, null, values) == -1)
			return false;
		db.close();

		return true;
	}

	// Adding new model
	public Boolean addListModel(List<GeneralModel> lmodel) {

		SQLiteDatabase db = this.getWritableDatabase();
		for (int i = 0; i < lmodel.size(); i++) {
			ContentValues values = lmodel.get(i).calculatorValue();
			if (db.insert(lmodel.get(i).tableName, null, values) == -1)
				return false;
		}

		db.close();

		return true;
	}

	// Getting single model
	public GeneralModel getModel(int id, ModelType type) {
		SQLiteDatabase db = this.getReadableDatabase();

		GeneralModel model = new GeneralModel(type);

		Cursor cursor = db.query(model.tableName, model.calculatorAttr(),
				"id=?", new String[] { String.valueOf(id) }, null, null, null,
				null);

		if (cursor == null || cursor.getCount() == 0)
			return null;
		else
			cursor.moveToFirst();

		model = gson.fromJson(cursor.getString(1), model.getClass());

		return model;
	}

	// Getting All feed
	public List<GeneralModel> getAllModel(ModelType type) {
		List<GeneralModel> modelList = new ArrayList<GeneralModel>();

		try {
			GeneralModel model = new GeneralModel(type);
			// Select All Query
			String selectQuery = "SELECT  * FROM " + model.tableName;

			SQLiteDatabase db = this.getWritableDatabase();
			Cursor cursor = db.rawQuery(selectQuery, null);

			if (cursor.moveToFirst()) {
				do {

					model = gson
							.fromJson(cursor.getString(1), model.getClass());
					modelList.add(model);
				} while (cursor.moveToNext());
			}
		} catch (Exception e) {

		}
		return modelList;
	}

	// Updating single feed
	public int updateModel(GeneralModel model) {
		SQLiteDatabase db = this.getWritableDatabase();

		// updating row
		return db.update(model.tableName, model.calculatorValue(), "id = ?",
				new String[] { model.getId() + "" });
	}

	// Deleting single feed
	public void deleteModel(GeneralModel model) {
		SQLiteDatabase db = this.getWritableDatabase();
		db.delete(model.tableName, "id = ?",
				new String[] { model.getId() + "" });
		db.close();
	}

}
